<template>
  <div class="userList">
    <div>
      <span>用户名：</span>
      <el-input v-model="accountNo" placeholder="请输入用户名" style="width: 200px;margin-right: 10px;"></el-input>
      <span>昵称：</span>
      <el-input v-model="userName" placeholder="请输入昵称" style="width: 200px;margin-right: 10px;"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
      <el-button type="primary" icon="el-icon-plus" @click="toAddPage" v-if="idAdmin">增加</el-button>
    </div>

    <el-table :data="tableData" border height="700" style="margin-top: 20px;">
      <el-table-column label="序号" width="80" >
        <template slot-scope="scope">{{ (pageInfo.pageNo - 1) * pageInfo.pageSize + scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column label="用户id" prop="id" v-if=false></el-table-column>
      <el-table-column label="用户名" prop="accountNo"></el-table-column>
      <el-table-column label="昵称" prop="userName"></el-table-column>
      <el-table-column label="性别" prop="sex"></el-table-column>
      <el-table-column label="年龄(岁)" prop="age"></el-table-column>
      <el-table-column label="养殖面积(亩)" prop="area"></el-table-column>
      <el-table-column label="电话" prop="telPhone"></el-table-column>
      <el-table-column label="注册日期" prop="createDateTime"></el-table-column>
      <el-table-column label="操作" v-if="idAdmin">
        <template slot-scope="scope">
          <i class="el-icon-edit actionIcon" style="color: #409eff;margin-right: 10px;"  @click="toEdit(scope.row)"></i>
          <i class="el-icon-delete actionIcon" style="color: #f56c6c;"  @click="handleDelete([scope.row.id])"></i>
        </template>
      </el-table-column>
    </el-table>
    
    <div style="margin-top: 20px;">
      <el-pagination
        background
        :current-page="pageInfo.pageNo"
        :page-size="pageInfo.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="pageInfo.total"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getUserPage, deleteById } from '@/api/user'

export default {
  name: 'UserList',
  data() {
    return {
      userName: '',
      accountNo: '',
      tableData: [],
      pageInfo: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  computed: {
    idAdmin() {
      const value = localStorage.getItem('account')
      return value && value === 'admin'
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      const payload = {
        pageNo: this.pageInfo.pageNo,
        pageSize: this.pageInfo.pageSize,
        accountNo: this.accountNo,
        userName: this.userName
      }
      getUserPage(payload).then(res => {
        console.log(res)
        this.tableData = res.data
        this.pageInfo.total = res.count
      })
    },
    toAddPage() {
      this.$router.push({ name: 'AddUser' })
    },
    toEdit(row) {
      this.$router.push({
        name: 'EditUser',
        params: {
          id: row.id
        },
      })
    },
    handleDelete(id) {
      this.$confirm('确认删除当前用户吗？').then(_ => {
        deleteById(id).then(res => {
          this.getList()
        })
      }).catch(_ => {})
    },
    handleCurrentChange(pageNo) {
      this.pageInfo.pageNo = pageNo;
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.userList {
  padding: 20px;
  .actionIcon {
    font-size: 18px;
    cursor: pointer;
  }
}
</style>